import request from "@/utils/request";
import { useTokenStore } from "@/stores/mytoken";

//登录接口的请求参数类型
type LoginInfo = {
    phone: string,
    password: string,
    code?: string
}
 //利用公共类型
type CommonReturn<T> = {
    success: boolean,
    state: number,
    message: string,
    content: T
}

 //用户登录返回数据类型
 type LoginResult = CommonReturn<string>

export const login =(loginInfo) =>{
    return request<LoginResult>({
        method: 'post',
        url: "/front/user/login",
        //不能有空格,要求请求类型为application/x-www-form-urlencoded
        data: `phone=${loginInfo.phone}&password=${loginInfo.password}`,
        //data: loginInfo, 
    });//data:`属性1=值1&属性2=值2`   用反引号包裹
        
    }

    //要求请求类型为application/json
    // 直接传  data: loginInfo
   
type UserInfo = CommonReturn<{  
    isUpdatedPassword: boolean,
    portrait: string,
    userName: string,
}>
    //获取用户信息
    
    export const getInfo = () => {
        return request<UserInfo>({
            method: 'get',
            url: "/front/user/getInfo",
        })
    }

    //退出登录
    export const logout = () => {

        return request({
            method: 'post',
            url: "/front/user/logout",
        })
    }
    //刷新token
    type RToken = CommonReturn<string>
//如果已经刷新过一次token  则不会多次刷新  直接把刷新过的返回
let promiseRT: Promise<any>
let isRefreshing = false
    export const refreshToken = () => {
        if (isRefreshing){
            return promiseRT
        }
        isRefreshing = true
        promiseRT = request<RToken>({
            method: 'post',
            url: "/front/user/refresh_token",
            params: {
                refreshtoken: useTokenStore().token?.refresh_token,
            },
        }).finally(() =>{
            isRefreshing = false
        })

        return promiseRT
    }


//公共的类型
type Common<T> = {
    code:string,
    data:T,
    mesg:string,
    time:string
}
//每一个用户项的类型
type UserItem = {
    accountNonExpired: boolean,
    accountNonLocked: boolean,
    createTime: string,
    credentialsNonExpired: boolean,
    id: number,
    isDel:boolean
    name: string,
    password: string,
    phone:string,
    portrait: string | null,
    regIp: string | null
    status: "ENABLE" | "DISABLE"
    updateTime: string,
}

//用户查询结果的类型
export type QueryResult = {
    current: number,
    hitCount: boolean,
    optimizeCountSql: boolean,
    orders: any[],
    pages: number,
    records:UserItem[]
    searchCount: boolean,
    size:number,
    total: number,
}

export type QueryCondition = Partial<{
    currentPage: number,
    pageSize: number,
    phone:string,
    userId:number
    startCreateTime:string,
    endCreateTime:string,
}>
export type UserRole = {
    hasPermission: boolean
    id: number,
    name: string,
}

    //获取用户列表信息
export const getUserPages = (queryCondition:QueryCondition = {}) => {
    return request<Common<QueryResult>>({
        method: 'post',
        url:'/boss/user/getUserPages',
        data: queryCondition,
    })
}

//启用用户
export const enableUser = (userId: number) => {
    return request<Common<UserRole[]>>({
        method:'get',
        url:'/boss/user/enableUser',
        params: {
            userId,
        },
    })
}


//禁用用户
export const forbidUser = (userId: number) => {
    return request<Common<boolean>>({
    method:'post',
    url:'/boss/user/forbidUser',
    data: {
        userId,
    },
    })
}

// 获取制定用户的角色信息
export const getRolesWithUserPermission = (userId: number) => {
    return request({
        method: 'get',
        url: '/boss/role/getRolesWithUserPermission',
        params: {
            userId,
        },
    })
}

//给制定用户分配角色
export const allocateUserRoles = (userId:number,roleIdList:number[]) => {
   return request<Common<boolean>>({
       method:'post',
       url:'/boss/role/allocateUserRoles',
       data: {
           userId,
           roleIdList,
       },
    })
}